<template>
  <el-dialog :visible.sync="dialogVisible" title="模型保存" @close="closeModal">
    <div style="margin-bottom: 10px;">
      <span>选择模型保存类型：</span>
      <el-radio-group v-model="radio">
        <el-radio :label="1">保存</el-radio>
        <el-radio :label="2">另存为</el-radio>
      </el-radio-group>
    </div>
    <el-form
      v-show="radio == 2"
      :model="modelForm"
      :rules="rule"
      ref="modelRef"
      size="small"
      label-position="right"
      label-width="160px"
    >
      <div class="flex" style="align-items: flex-start;">
        <div class="inputContainer">
          <el-form-item label="模型名称：" prop="mxmc">
            <el-input
              v-model="modelForm.mxmc"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入模型名称"
            ></el-input>
          </el-form-item>

          <el-form-item label="AI框架：" prop="aikj">
            <el-input
              v-model="modelForm.aikj"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入AI框架"
            ></el-input>
          </el-form-item>

          <el-form-item label="运行环境：" prop="yxhj">
            <el-input
              v-model="modelForm.yxhj"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入运行环境"
            ></el-input>
          </el-form-item>

          <el-form-item label="决策速度：" prop="jcsd">
            <el-input
              readonly
              v-model="modelForm.jcsd"
              v-only-number
              v-max-length-tip="10"
              v-no-space
              placeholder="请输入决策速度"
            ></el-input>
          </el-form-item>

          <el-form-item label="算法名称：" prop="sfmc">
            <el-input
              v-model="modelForm.sfmc"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入算法名称"
            ></el-input>
          </el-form-item>
        </div>

        <div class="inputContainer">
          <el-form-item label="开发语言：" prop="kfyy">
            <el-select v-model="modelForm.kfyy" placeholder="请输入开发语言">
              <el-option
                v-for="(item, index) in kfyyOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="版本信息：" prop="bbxx">
            <el-input
              v-model="modelForm.bbxx"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入版本信息"
            ></el-input>
          </el-form-item>
          <el-form-item label="总体准确率：" prop="jczql">
            <el-input
              readonly
              v-model="modelForm.jczql"
              v-only-number
              v-max-length-tip="10"
              v-no-space
              placeholder="请输入总体准确率"
            ></el-input>
          </el-form-item>
          <el-form-item label="模型类型：" prop="mxlx">
            <el-input
              v-model="modelForm.mxlx"
              v-max-length-tip="30"
              v-no-space
              placeholder="请输入模型类型"
            ></el-input>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('modelRef')">确定</el-button>
      <el-button @click="closeModal">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    decisionCallback: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      dialogVisible: false,
      modelForm: {
        mxmc: '',
        aikj: '',
        yxhj: '',
        jcsd: '',
        sfmc: '',
        kfyy: '',
        bbxx: '',
        jczql: '',
        mxlx: ''
      },
      radio: 1,
      rule: {
        radio: [
          { required: true, message: '请选择模型保存类型', trigger: 'change' }
        ],
        mxmc: [{ required: true, message: '请输入模型名称', trigger: 'blur' }],
        aikj: [{ required: true, message: '请输入AI框架', trigger: 'blur' }],
        yxhj: [{ required: true, message: '请输入运行环境', trigger: 'blur' }],
        sfmc: [{ required: true, message: '请输入算法名称', trigger: 'blur' }],
        kfyy: [{ required: true, message: '请输入开发语言', trigger: 'blur' }],
        bbxx: [{ required: true, message: '请输入版本信息', trigger: 'blur' }],
        mxlx: [{ required: true, message: '请输入模型类型', trigger: 'blur' }]
      },
      kfyyOptions: [
        {
          value: 'Java',
          label: 'Java'
        },
        {
          value: 'Python',
          label: 'Python'
        },
        {
          value: 'C',
          label: 'C/C++'
        }
      ]
    }
  },
  methods: {
    submitForm(ref) {
      // 是保存，直接提交
      if (this.radio == 1) {
        this.dialogVisible = false
        this.$emit('decisionCallback', this.modelForm, this.radio)
        return
      }
      // 是另存为，需要校验
      this.$refs[ref].validate(valid => {
        if (valid) {
          this.dialogVisible = false
          this.$emit('decisionCallback', this.modelForm, this.radio)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    openModal(accuracy, speed) {
      this.dialogVisible = true
      const rowInfo = JSON.parse(sessionStorage.getItem('decisionInfo'))
      this.modelForm = rowInfo
      this.modelForm.jczql = accuracy
      this.modelForm.jcsd = speed
      this.radio = 1
    },
    closeModal() {
      if (this.$refs.modelRef) {
        this.$refs.modelRef.resetFields()
      }
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table__expanded-cell {
  padding: 0 55px;
}

::v-deep .el-table__body tr:hover > td {
  background-color: transparent !important;
}

::v-deep .el-table .el-table__header-wrapper thead tr th {
  background-color: transparent;
}

::v-deep .el-input--small .el-input__inner {
  border-color: #2d65ad !important;
}

::v-deep .el-select {
  width: 100%;
}
.inputContainer {
  width: 50%;
}
</style>
